package net.oschina.app.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.MApp;
import net.oschina.app.R;
import net.oschina.app.adapter.NewsItemCommentAdapter;
import net.oschina.app.bean.CommentList;
import net.oschina.app.util.XmlUtils;

import java.io.IOException;
import java.io.InputStream;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Ethan on 2017/4/7.
 */
public class CommentDetailActivity extends AppCompatActivity {
    @BindView(R.id.tb_news)
    Toolbar mTbNews;
    @BindView(R.id.xrv_comment_list)
    XRecyclerView mXrvCommentList;
    @BindView(R.id.et_input)
    EditText mEtInput;
    @BindView(R.id.ib_emoji)
    ImageButton mIbEmoji;
    @BindView(R.id.ib_biz_chat)
    ImageButton mIbBizChat;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment_detail);
        ButterKnife.bind(this);

        //设置Toolbar显示
        setSupportActionBar(mTbNews);

        //设置Toolbar显示导航键(返回键)
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //设置Toolbar点击导航键返回,并销毁当前页面
        mTbNews.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //获取网络数据
        //根据获取的id数据拼接url
        String url = "http://10.0.2.2:8080/oschina/commentlist/catalog1id64268page0.xml";

        //创建client对象
        OkHttpClient client = new OkHttpClient();

        //根据url构建请求
        Request request = new Request.Builder().url(url).build();

        //根据请求获得Call对象
        Call call = client.newCall(request);

        //根据call对象异步获取网络数据
        call.enqueue(new Callback() {
            //获取数据失败执行的方法
            @Override
            public void onFailure(Call call, IOException e) {

            }

            //获取数据成功响应的方法
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //根据响应获取响应流
                InputStream inputStream = response.body().byteStream();

                //通过XML解析工具,传入对应的Bean类字节码文件对象,把响应流中的数据解析成对应的实体Bean
                final CommentList commentList = XmlUtils.toBean(CommentList.class, inputStream);

                //通过解析出来的实体Bean获取展示界面需要的信息,然后将信息展示在界面(注意:此为UI更改操作,需确保在UI线程中执行)
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LinearLayoutManager manager=new LinearLayoutManager(MApp.mContext);
                        mXrvCommentList.setLayoutManager(manager);
                        NewsItemCommentAdapter adapter=new NewsItemCommentAdapter(MApp.mContext,
                                commentList);
                        mXrvCommentList.setAdapter(adapter);
                    }
                });
            }
        });
    }
}
